package com.jcl.recruit.api.verify.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.verfity.service.VerificationService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName VerificationController
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/18 9:48
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/verify")
public class VerificationController {

    private static final Logger logger = LoggerFactory.getLogger(VerificationController.class);

    @Autowired
    private VerificationService verificationService;

    @RequestMapping("/email/registercode")
    public ApiResponse sendEmailCode(String email) {

        if(StringUtils.isEmpty(email)){
            return ApiResponse.error(Constants.ReturnInfo.ERROR_PARAM_NULL_MSG);
        }
        int success = verificationService.sendEmailRegisterCode(email);
        if(success == 0){
            return ApiResponse.success(Constants.ReturnInfo.SUCCESS_MSG);
        }else if(success == 1){
            return ApiResponse.error(Constants.ReturnInfo.ERROR_MSG);
        }else{
            return ApiResponse.error(Constants.ReturnInfo.ERROR_MSG);
        }

    }
    @RequestMapping("/phone/registercode")
    public ApiResponse sendPhoneCode(String phone) {

        if(StringUtils.isEmpty(phone)){
            return ApiResponse.error(Constants.ReturnInfo.ERROR_PARAM_NULL_MSG);
        }
        int success = verificationService.sendPhoneRegisterCode(phone);
        if(success == 1){
            return ApiResponse.success(Constants.ReturnInfo.ERROR_MSG);
        }else if(success == 0){
            return ApiResponse.error(Constants.ReturnInfo.SUCCESS_MSG);
        }

        return ApiResponse.error(Constants.ReturnInfo.ERROR_MSG);
    }

    @RequestMapping("/email/registercode/validate")
    public ApiResponse sendEmailCodeValidate(String email, String code) {

        int success = verificationService.validateEmailRegisterCode(email, code);
        if(success == 1){
            return ApiResponse.error("验证码错误");
        }else if(success == 2){
            return ApiResponse.error("验证码失效");
        }else{
            return ApiResponse.success(Constants.ReturnInfo.SUCCESS_MSG);
        }

    }

    @RequestMapping("/phone/registercode/validate")
    public ApiResponse sendPhoneCodeValidate(String phone, String code) {

        int success = verificationService.validatePhoneRegisterCode(phone, code);
        if(success == 1){
            return ApiResponse.error("验证码错误");
        }else if(success == 2){
            return ApiResponse.error("验证码失效");
        }

        return ApiResponse.success(Constants.ReturnInfo.SUCCESS_MSG);
    }
}
